package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.Customer;
import com.scm.biz.bean.DesignOrder;
import com.scm.biz.bean.Order;
import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.dao.CustomerDao;
import com.scm.biz.dao.DesignOrderDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderPatternDao;
import com.scm.biz.dao.OrderTaskPerformerDao;
import com.scm.biz.dao.SysUserDao;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.query.DesignOrderQuery;
import com.scm.biz.result.DesignOrderResult;
import com.scm.biz.service.DesignOrderService;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.UfdmDateUtil;

@Service
public class DesignOrderServiceImpl implements DesignOrderService {

	@Resource
	private OrderDao orderDao;
	@Resource
	private DesignOrderDao designOrderDao;
	@Resource
	private SysUserDao sysUserDao;
	@Resource
	private OrderTaskFlowService orderTaskFlowService;
	@Autowired
	OrderPatternDao orderPatternDao;
	@Autowired
	MsgNoticeService msgNoticeService;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;

	@Override
	public void addDesignOrder(DesignOrder designOrder, Order order) {
		// 创建后设计订单状态
		designOrder.setStatus(OrderStatusEnum.DESIGN_CREATED.value());
		designOrder.setCreateAt(new Date());

		orderDao.insertReturnId(order);
		designOrder.setOrderId(order.getId());
		designOrderDao.insertSelective(designOrder);
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F100");
		orderTaskPerformer.setBusinessId(designOrder.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T1.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(designOrder.getCreator());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(order.getId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		
		orderTaskFlowService.addPerformer(OrderTypeEnum.T1,"F101", "R001", designOrder.getId());//设计总监
		orderTaskFlowService.addPerformer(OrderTypeEnum.T1,"F109", "R007", designOrder.getId());//供应链总监
		/**
		 * 添加消息
		 */
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("新的设计订单");
		msgNoticeDto.setContent(sysUserDao.selectByPrimaryKey(designOrder.getCreator()).getName() + "创建了" + customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的设计订单，请尽快安排设计，截止时间为" + UfdmDateUtil.dateToString(designOrder.getDesignEnddate(), "MM月dd日"));
		msgNoticeDto.setEnddate(designOrder.getDesignEnddate());
		msgNoticeDto.setOrderType(OrderTypeEnum.T1.toString());
		msgNoticeDto.setTaskId(designOrder.getId());
		msgNoticeDto.setUserType(1);
		msgNoticeDto.setOrderId(order.getId());
		msgNoticeService.insert(msgNoticeDto,"R001");
		

	}

	@Override
	public DesignOrder selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return designOrderDao.selectByPrimaryKey(id);
	}

	@Override
	public List<DesignOrderResult> selectPage(DesignOrderQuery designOrderQuery) {
		return designOrderDao.selectPage(designOrderQuery);
	}

	@Override
	public DesignOrderResult selectById(Long id) {
		return designOrderDao.selectDesignPageById(id);
	}

	@Override
	public void saveAssignUser(List<Long> businessIds, Integer assignUser) throws UfdmException {
		for (Long businessId : businessIds) {
			DesignOrder designOrder = designOrderDao.selectByPrimaryKey(businessId);
			designOrder.setDesigner(assignUser);
			if (designOrder.getStatus() < OrderStatusEnum.DSIGN_VERIFING.value()) {
				designOrder.setStatus(OrderStatusEnum.DESIGN_ING.value());
				orderTaskFlowService.addPerformer(OrderTypeEnum.T1, "F102", assignUser, businessId);
			}else{
				throw new UfdmException("订单状态正在审核中，不可更改设计师");
			}

			designOrderDao.updateByPrimaryKey(designOrder);
			
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F101");
			orderTaskPerformer.setBusinessId(designOrder.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T1.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(designOrder.getCreator());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(designOrder.getOrderId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
			/**
			 * 添加消息
			 */
			Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());

			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("新的设计订单");
			msgNoticeDto.setContent(sysUserDao.selectByPrimaryKey(userId).getName() + "指派了" + customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(designOrder.getOrderId()).getCustomerId().intValue()).getFullname()
									+ "客户的设计订单给你，请尽快设计，截止时间为" + UfdmDateUtil.dateToString(designOrder.getDesignEnddate(), "MM月dd日"));
			msgNoticeDto.setEnddate(designOrder.getDesignEnddate());
			msgNoticeDto.setOrderType(OrderTypeEnum.T1.toString());
			msgNoticeDto.setTaskId(designOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(designOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,assignUser);
			
		}
	}
	/**
	 * 提交审核
	 */
	@Override
	public void saveSubmitVerify(Long businessId, Long orderId) {
		orderTaskFlowService.addPerformer(OrderTypeEnum.T1,"F105", "R001", businessId);//提交给设计总监审核
		DesignOrder designOrder = designOrderDao.selectByPrimaryKey(businessId);
		designOrder.setStatus(OrderStatusEnum.DSIGN_VERIFING.value());
		designOrderDao.updateByPrimaryKey(designOrder);
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());

		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F105");
		orderTaskPerformer.setBusinessId(designOrder.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T1.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(userId);
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(designOrder.getOrderId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		
		
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("设计订单待审核");
		msgNoticeDto.setContent(sysUserDao.selectByPrimaryKey(userId).getName() + "提交了" + customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(designOrder.getOrderId()).getCustomerId().intValue()).getFullname()
								+ "客户的设计稿，请尽快审核");
		msgNoticeDto.setEnddate(designOrder.getDesignEnddate());
		msgNoticeDto.setOrderType(OrderTypeEnum.T1.toString());
		msgNoticeDto.setTaskId(designOrder.getId());
		msgNoticeDto.setUserType(1);
		msgNoticeDto.setOrderId(designOrder.getOrderId());
		msgNoticeService.insert(msgNoticeDto,"R001");
	}

	/**
	 * 审核通过
	 */
	@Override
	public void saveVerify(Long businessId, Long orderId) {
		//orderTaskFlowService.addPerformer(OrderTypeEnum.T1, "F104,F100", businessId);
		DesignOrder designOrder = designOrderDao.selectByPrimaryKey(businessId);
		designOrder.setStatus(OrderStatusEnum.FINISH.value());
		designOrderDao.updateByPrimaryKey(designOrder);
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F103");
		orderTaskPerformer.setBusinessId(designOrder.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T1.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(designOrder.getCreator());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(designOrder.getOrderId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		
		
		
		Order order = orderDao.selectByPrimaryKey(designOrder.getOrderId());
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("新的设计订单");
		msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的设计稿已完成，请尽快联系客户");
		msgNoticeDto.setEnddate(designOrder.getDesignEnddate());
		msgNoticeDto.setOrderType(OrderTypeEnum.T1.toString());
		msgNoticeDto.setTaskId(designOrder.getId());
		msgNoticeDto.setUserType(0);
		msgNoticeDto.setOrderId(designOrder.getOrderId());
		msgNoticeService.insert(msgNoticeDto,designOrder.getCreator());
	}

	/**
	 * 关闭订单
	 */
	@Override
	public void saveClose(Long businessId, Long orderId) {
		//orderTaskFlowService.addPerformer(OrderTypeEnum.T1, "", businessId);
		DesignOrder designOrder = designOrderDao.selectByPrimaryKey(businessId);
		designOrder.setStatus(OrderStatusEnum.CLOSE.value());
		designOrderDao.updateByPrimaryKey(designOrder);
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F104");
		orderTaskPerformer.setBusinessId(designOrder.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T1.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(designOrder.getCreator());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(orderId);
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		
		Order order = orderDao.selectByPrimaryKey(designOrder.getOrderId());
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("设计稿未通过选款");
		msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的设计稿未通过选稿");
		msgNoticeDto.setEnddate(designOrder.getDesignEnddate());
		msgNoticeDto.setOrderType(OrderTypeEnum.T1.toString());
		msgNoticeDto.setTaskId(designOrder.getId());
		msgNoticeDto.setOrderId(designOrder.getOrderId());
		msgNoticeDto.setUserType(0);
		msgNoticeService.insert(msgNoticeDto,designOrder.getDesigner());
		msgNoticeDto.setUserType(1);
		msgNoticeService.insert(msgNoticeDto,"R001");
	}

	@Override
	public int selectTotal(DesignOrderQuery designOrderQuery) {
		return designOrderDao.selectTotal(designOrderQuery);
	}

	@Override
	public DesignOrder selectByOrderId(Long orderId) {
		// TODO Auto-generated method stub
		return designOrderDao.selectByOrderId(orderId);
	}

}
